#include "../skyAdapter.h"
#include "../skyFile.h"
#ifdef WIN32


#include "../skyOS.h"
#include <strsafe.h>
namespace Sky
{

    TBool File::open( const StringPtr& name,OpenFlags flag )
    {
		StringPtr ptr = const_cast<StringPtr&>(name);
		_name = ptr;
        hFile = CreateFile(_name->toCharString(),GENERIC_READ,0,0,OPEN_EXISTING,0,0);
        return hFile != INVALID_HANDLE_VALUE ? vTrue : vFalse;
    }

    void File::close()
    {
        _bOpen = vFalse;
        CloseHandle(hFile);
        hFile = vNull;
    }

    Sky::NType::TBool File::seekToEnd()
    {
        return vFalse;
    }

    Sky::NType::TBool File::seekToBegin()
    {
        return vFalse;
    }

    Sky::NType::TUint File::getLength() const
    {
        return 0;
    }

    Sky::NType::TBool File::seek( TInt lOff, TUint nFrom )
    {
        return 0;
    }

    Sky::NType::TInt File::write( const u8* lpBuf, TUint nCount )
    {
        DWORD written;
        return vTrue == WriteFile(hFile,lpBuf,nCount,(LPDWORD)&written,vNull) ? written : -1;
    }

    Sky::NType::TInt File::read( u8* lpBuf, TUint nCount )
    {
        DWORD read;
        return vTrue == ReadFile(hFile,lpBuf,nCount,(LPDWORD)&read,vNull) ? read : -1;
    }

}
namespace Sky
{
    OSFile::OSFile():
    hFile(vNull)
    {

    }

    TInt OSFile::readLine( u8* buff,TUint buffLen )
    {
	    u8 ch;
	    DWORD r;
	    //for(;;)
	    //{
		   // if(vFalse == ReadFile(hFile,&ch,sizeof(ch),&r,vNull))
			  //  return -1;
		   // if(Sky::NHelper::Helper::isVisible(ch))
			  //  break;
	    //}
    	//
	    //TUint n = 0;
	    //for(;;)
	    //{
		   // if(n == buffLen)
			  //  return -1;
		   // if(!Sky::NHelper::Helper::isVisible(ch))
		   // {
			  //  *read = n;
			  //  return read;
		   // }
		   // buff[n] = ch;
		   // n++;
		   // if(vFalse == ReadFile(hFile,&ch,sizeof(ch),&r,vNull))
			  //  return -1;
	    //}
	    return -1;
    }

    OSDirectory::OSDirectory():
    hListFile(INVALID_HANDLE_VALUE)
    {
    
    }

    const StringPtr OSDirectory::listFileInDir( const StringPtr& dir,const StringPtr& fileMask,TBool isFirst /*= vTrue*/,StringPtr& out )
    {
		StringPtr& dirPath = const_cast<StringPtr&>(dir);
    	if(isFirst)
    	{
    		TChar szFiles[MAX_PATH];
    		if( GetFileAttributes( dirPath->toCharString() ) == 0xFFFFFFFF )
    		{
    			//StringCchCopy(dirRet,dirRetLen,strSearchPath);
    			return out;
    		}
    		StringCchPrintf( szFiles, sizeof(szFiles), _XT("%s\\%s"),dirPath->toCharString(),const_cast<StringPtr&>(fileMask)->toCharString() ); 
    
    		hListFile = FindFirstFile(szFiles,&FindFileData);
    		if(hListFile == INVALID_HANDLE_VALUE)
    			return out;
            out->copyFrom(FindFileData.cFileName);
    	}
    	else
    	{
    		if(FindNextFile(hListFile, &FindFileData))
            {
    			out->copyFrom(FindFileData.cFileName);
            }
    	}
    	return out;	
    }
}

#endif


